<?php

namespace app\api\controller\xykeep;

use app\common\controller\Api;
use app\api\model\xykeep\Config;
use app\common\model\Area;
use app\common\model\Subject;
use app\common\model\Grade;
use app\common\model\AgeRange;
use think\Cache;
use think\Config as ThinkConfig;
use think\view\driver\Think;

/**
 * 通用接口
 */
class Common extends Api
{
	protected $noNeedLogin = ['init','cityData'];
	protected $noNeedRight = ['*'];


	/**
	 * 加载目标信息和通用配置
	 */
	public function init()
	{

        $customConfig = [
            'subject' => (new Subject())->select(),
            'grade' => (new Grade())->select(),
            'grade_normal' => (new Grade())->where([
                'name' => ['neq','不限']
            ])->select(),
            'age_range' => (new AgeRange())->order('weigh desc,id desc')->select(),
            'settled_type_free' => json_decode(ThinkConfig::get('site.settled_type_free'),true),
            'settled_type' => json_decode(ThinkConfig::get('site.settled_type'),true),
            'settled_price' => ThinkConfig::get('site.settled_price'),
            'teaching_method' => json_decode(ThinkConfig::get('site.teaching_method'),true),
            'deposit_money' => ThinkConfig::get('site.deposit_money'),
            'agency_money' => ThinkConfig::get('site.agency_money'),
            'education' => json_decode(ThinkConfig::get('site.education'),true),
            'group' => json_decode(ThinkConfig::get('site.group'),true),
            'year_free' => json_decode(ThinkConfig::get('site.year_free'),true),
            'feedback' => json_decode(ThinkConfig::get('site.feedback')),
            'balance_on' => json_decode(ThinkConfig::get('site.balance_on')),
            'abnormal_order' => ThinkConfig::get('site.abnormal_order'),
            'accept_job_rule' => ThinkConfig::get('site.accept_job_rule'),
            'accept_job_deposit' => ThinkConfig::get('site.accept_job_deposit'),
            'map_center_lat' => ThinkConfig::get('site.map_center_lat'),
            'map_center_lng' => ThinkConfig::get('site.map_center_lng'),
            'staff_pass_notice' => ThinkConfig::get('site.staff_pass_notice'),
            'staff_refuse_notice' => ThinkConfig::get('site.staff_refuse_notice'),
            'job_cancel_notice' => ThinkConfig::get('site.job_cancel_notice'),
            'job_over_notice' => ThinkConfig::get('site.job_over_notice'),
            'parent_interven_notice' => ThinkConfig::get('site.parent_interven_notice'),
            'staff_interven_notice' => ThinkConfig::get('site.staff_interven_notice'),
            'staff_over_notice' => ThinkConfig::get('site.staff_over_notice'),
            'job_push_for_parent' => ThinkConfig::get('site.job_push_for_parent'),
            'job_teacher_cancel_notice' => ThinkConfig::get('site.job_teacher_cancel_notice'),
        ];

        $auditConfig = [
            'audit_on' => ThinkConfig::get('site.audit_on'),

            'index_sgin_in1' => ThinkConfig::get('site.index_sgin_in1'),
            'index_sgin_in2' => ThinkConfig::get('site.index_sgin_in2'),
            'index_sgin_in3' => ThinkConfig::get('site.index_sgin_in3'),
            'index_sgin_in4' => ThinkConfig::get('site.index_sgin_in4'),

            'index_find1' => ThinkConfig::get('site.index_find1'),
            'index_find2' => ThinkConfig::get('site.index_find2'),
            'index_find3' => ThinkConfig::get('site.index_find3'),
            'index_find4' => ThinkConfig::get('site.index_find4'),
            'index_find5' => ThinkConfig::get('site.index_find5'),
            'index_find6' => ThinkConfig::get('site.index_find6'),
            'index_find7' => ThinkConfig::get('site.index_find7'),
            'index_find8' => ThinkConfig::get('site.index_find8'),

            'index_teachers' => ThinkConfig::get('site.index_teachers'),
            'index_help1' => ThinkConfig::get('site.index_help1'),
            'index_help2' => ThinkConfig::get('site.index_help2'),
            'index_help3' => ThinkConfig::get('site.index_help3'),

            'index_school1' => ThinkConfig::get('site.index_school1'),
            'index_school2' => ThinkConfig::get('site.index_school2'),
            'index_school3' => ThinkConfig::get('site.index_school3'),
            'index_school4' => ThinkConfig::get('site.index_school4'),
            'index_school5' => ThinkConfig::get('site.index_school5'),
            'index_app_title' => ThinkConfig::get('site.index_app_title'),

            'pages_teacher' => ThinkConfig::get('site.pages_teacher'),
            'pages_teacher_detail' => ThinkConfig::get('site.pages_teacher_detail'),
            'pages_teacher_apply' => ThinkConfig::get('site.pages_teacher_apply'),
            'pages_user_collect' => ThinkConfig::get('site.pages_user_collect'),
            'pages_user_teacher' => ThinkConfig::get('site.pages_user_teacher'),

            'user_student' => ThinkConfig::get('site.user_student'),
            'user_teacher' => ThinkConfig::get('site.user_teacher'),
            'user_share' => ThinkConfig::get('site.user_share'),

            'settled_notice1' => ThinkConfig::get('site.settled_notice1'),
            'settled_notice2' => ThinkConfig::get('site.settled_notice2'),
        ];
		$this->success('init', [
			"appStyle" => Config::getValueByName('appstyle'),
			"appConfig" => Config::getValueByName('xykeep'),
			"shareConfig" => Config::getValueByName('share'),
			"tabBarList"  => Config::getValueByName('tabbar'),
            'customConfig' => $customConfig,
            'auditConfig' => $auditConfig,
		]);
	}

	/**
     * 根据经纬度获取城市数据
     *
     * @param string $lng     经度
     * @param string $lat     纬度
     */
    public function cityData()
    {
		$lng = $this->request->request('lng');
		$lat = $this->request->request('lat');

		// 添加缓存标记，避免重复执行
		$level = 3;
        if (Cache::tag('geo')->get('geo:area_level_' . $level) !== true) {
            self::area2RedisGeo($level);
        }
		$this->success('城市信息', Area::getCityFromLngLat($lng, $lat));
    }

	/**
     * 区域经纬度到redis
    */
    private function area2RedisGeo($level) {
        $areas = Area::where(['level' => $level])->select();
        $namearr = [1 => 'geo:province', 2 => 'geo:city', 3 => 'geo:district'];
        $redis = Cache::store('redis')->handler();
        foreach ($areas as $area) {
            if (method_exists($redis, 'geoadd')) {
                $redis->geoadd($namearr[$area['level']], $area['lng'], $area['lat'], $area['id']);
            }
        }
        Cache::tag('geo')->set('geo:area_level_' . $level, true);
    }

	/**
	 * 省市区
	 */
	public function area()
    {
        $data['provinceData'] = Area::where('level', 1)->order('id asc')->field('id as value, name as label, pid, level')->select();
        foreach ($data['provinceData'] as $k => $p) {
            $data['cityData'][$k] = Area::where(['level' => 2, 'pid' => $p->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
            foreach ($data['cityData'][$k] as $i => $c) {
                $data['areaData'][$k][$i] = Area::where(['level' => 3, 'pid' => $c->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
            }
        }

        $this->success('省市区', $data);

    }

}
